#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;

// 票数：10张
int g_count = 10;

/* 线程：
    共享：全局变量
    私有：栈空间
*/
void *thread_func1(void *arg)
{
    cout << "thread1 cnt:" << g_count << endl;

    // g_count += 10;

    // int iphone = 5499;

    while (g_count > 0)
    {
        sleep(1);

        // 下单
        g_count--;
        cout << "thread1 cnt:" << g_count << endl;
    }
    // 线程退出
    pthread_exit(NULL);
}

void *thread_func2(void *arg)
{
    while (g_count > 0)
    {
        sleep(1);
        
        // 下单
        g_count--;
        cout << "thread1 cnt:" << g_count << endl;
    }
    // cout << "thread2 cnt:" << g_count << endl;

    // cout << "iphone:" << iphone << endl;

    // 线程退出
    pthread_exit(NULL);
}

void *thread_func3(void *arg)
{
    while (g_count > 0)
    {
        sleep(1);
        
        // 下单
        g_count--;
        cout << "thread1 cnt:" << g_count << endl;
    }
    // cout << "thread3 cnt:" << g_count << endl;

    // 线程退出
    pthread_exit(NULL);
}

int main()
{
    pthread_t tid1;
    pthread_create(&tid1, NULL, thread_func1, NULL);

    pthread_t tid2;
    pthread_create(&tid2, NULL, thread_func2, NULL);

    pthread_t tid3;
    pthread_create(&tid3, NULL, thread_func3, NULL);

    while (1)
    {
        sleep(1);
    }

    return 0;
}